<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

class AdgroupAction extends AccountAction
{
	public static $semMethod = array(
					'sem_total_num' 	=> array(
							'table' => 'semiautotag',
							'where' => array(
								'platform' 			=> array('IN', '0,2')
							)
						),
					'sem_custom_num' 	=> array(
							'table' => 'semiautotag',
							'where' => array(
								'type' 				=> 3,
								'platform' 			=> 0
							)
						),
					'sem_public_num' 	=> array(
							'table' => 'semiautotag',
							'where' => array(
								'type'				=> 4,
								'platform'			=> 0
							)
						),
					'kawei_total_num' 	=> array(
							'table' => 'tactics',
							'where' => array()
						),
					'kawei_use_num' 	=> array(
							'table' => 'tactics',
							'where' => array(
								'tactics_status' 	=> 1
							)
						)
		);

	public static $campaignMethod = array(
					'auto_cam_num' 	=> array(
							'table' => array('adgroups'=> 'a'),
							'join' => 'RIGHT JOIN campaigns c ON a.campaign_id = c.campaign_id',
							'where' => array(
								'drive_type' 		=> 1,
								'c.title' => array('LIKE', '%云智投%')
							)
						),
					'wireless_cam_num' 	=> array(
							'table' => array('adgroups'=> 'a'),
							'join' => 'RIGHT JOIN campaigns c ON a.campaign_id = c.campaign_id',
							'where' => array(
								'drive_type' 		=> 2
							)
						),
					'cam_num' 	=> array(
							'table' => array('campaigns'=> 'c'),
							'join' 	=> false,
							'where' => array(
								'c.title' => array('LIKE', '%云智投%')
							)
						)
		);

	public function index()
	{
		$page = I('page', 1);
		$page = (int)$page > 0 ? $page : 1;

		$pageSize = 50;
		$size = ($page-1)*$pageSize;

		$order = I('order');
		switch ($order) {
			case 'sem_total_num':
			case 'sem_custom_num':
			case 'sem_public_num':
			case 'sem_use_num':
			case 'kawei_total_num':
			case 'kawei_use_num':
			case 'cam_num':
			case 'auto_cam_num':
			case 'wireless_cam_num':
				break;
			default:
				$order = 'sem_total_num';
				break;
		}

		$asc = strtolower(I('asc'));
		$asc = $asc == 'asc' ? 'asc' : 'desc';
		
		$where = I('search');
		$params = 'u.item_code != \'\'';
		if(isset($where['from']) && $where['from']) {
			$from = trim($where['from']);
			unset($where['from']);
		} else {
			$from = date('Y-m-d', strtotime('-7 day'));
		}

		if(isset($where['to']) && $where['to']) {
			$to = trim($where['to']);
			unset($where['to']);
		} else {
			$to = date('Y-m-d', strtotime('-1 day'));
		}
		$dateTime = "AND s.add_time BETWEEN '{$from}' AND '{$to}'";

		if(isset($where['nick']) && $where['nick']) {
			$nick = trim($where['nick']);
			$this->assign('nick', $nick);
			$params .= " AND u.nick LIKE '%{$nick}%'";
			unset($nick);
		}

		$users = M()->field('u.user_id,u.nick,COUNT(s.id) sem_use_num')
					->table(array('users'=>'u'))
					->join("LEFT JOIN semiautotag_log s ON s.user_id = u.user_id {$dateTime}")
					->where($params)
					->group('u.user_id')
					->select(array('assoc_key' => 'user_id'));
		$user_iids = array_keys($users);
		
		$semTableData = array();
		foreach (self::$semMethod as $method => $items) {
			$item = array_merge(array('user_id' => array('IN', $user_iids)), $items['where']);
			$tmp = M($items['table']);
			$semTableData[$method] = $tmp->field("user_id, count(".$tmp->getPk().") {$method}")
				   					  	 ->where($item)
				   					  	 ->group('user_id')
				   					  	 ->select(array('assoc_key' => 'user_id'));
		}
		unset($item);unset($tmp);unset($where);unset($params);

		
		$campaignTableData = array();
		foreach (self::$campaignMethod as $method => $items) {
			$item = array_merge(array('c.user_id' => array('IN', $user_iids)), $items['where']);
			$table = array_keys($items['table']);
			$alias = array_values($items['table']);
			$tmp = M($table[0]);
			$tmp->alias($alias[0]);
			if($items['join'] !== false)
				$tmp->join($items['join']);
			$campaignTableData[$method] = $tmp->field("c.user_id, count(DISTINCT c.campaign_id) {$method}")
										   ->where($item)
										   ->group('c.user_id')
										   ->select(array('assoc_key' => 'user_id'));
		}

		foreach ($users as $user_id => &$user) {
			$user['sem_use_num'] = (int)$user['sem_use_num'];
			foreach ($semTableData as $method => $items) {
				$user[$method] = isset($items[$user_id]) ? (int)$items[$user_id][$method] : 0;
			}
			foreach ($campaignTableData as $method => $items) {
				$user[$method] = isset($items[$user_id]) ? (int)$items[$user_id][$method] : 0;
			}
		}

		$total = array(
				'total_user_num' => count($users),
				'total_sem_num' => array_sum(array_column($users, 'sem_total_num')),
				'total_sem_public_num' => array_sum(array_column($users, 'sem_public_num')),
				'total_sem_custom_num' => array_sum(array_column($users, 'sem_custom_num')),
				'total_sem_use_num' => array_sum(array_column($users, 'sem_use_num')),
				'total_kawei_num' => array_sum(array_column($users, 'kawei_total_num')),
				'total_kawei_use_num' => array_sum(array_column($users, 'kawei_use_num')),
				'total_campaign_num' => array_sum(array_column($users, 'cam_num')),
				'total_campaign_auto_num' => array_sum(array_column($users, 'auto_cam_num')),
				'total_campaign_wireless_num' => array_sum(array_column($users, 'wireless_cam_num')),
			);
		unset($tableData);

		$totalCount = count($users);
		$totalPage = ceil($totalCount/$pageSize);

		$func = "uc_cmp_{$order}_{$asc}";
		if($order)  {
			usort($users, $func);
		}

		$users = array_slice($users, $size, $pageSize);

		$query = array();
		if(I('search'))
			$query['search'] = I('search');
		$query['order'] = $order;
		$query['asc'] = $asc;

		$this->assign('users', $users);

		$this->assign('dateTime', array('from'=> $from, 'to'=> $to));

		$this->assign('order', $order);
		$this->assign('asc', strtoupper($asc));

		$this->assign('totalPage', $totalPage);
		$this->assign('lastPage', $page <= 1 ? 1 : $page-1);
		$this->assign('currentPage', $page);
		$this->assign('nextPage', $page >= $totalPage ? $totalPage : $page+1);
		$this->assign('current_search', $query ? ('&' . http_build_query($query)) : '');
		$this->assign('total', $total);

		$this->display();
	}
}